package top.chukongxiang.project.base.model.response;

public class R<T> {

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 描述信息
     */
    private String msg;

    /**
     * 返回结果数据
     */
    private T data;


    /**
     * 执行成功
     */
    public static R ok() {
        R result = new R();
        result.setCode(RCode.SUCCESS.getCode());
        result.setMsg(RCode.SUCCESS.getMsg());
        result.setData("");
        return result;
    }

    /**
     * 执行成功，并返回结果集
     */
    public static <T> R<T> ok(T data) {
        R<T> result = new R<T>();
        result.setCode(RCode.SUCCESS.getCode());
        result.setMsg(RCode.SUCCESS.getMsg());
        result.setData(data);
        return result;
    }

    /**
     * 执行失败
     */
    public static R failed() {
        R result = new R();
        result.setCode(RCode.FAILED.getCode());
        result.setMsg(RCode.FAILED.getMsg());
        result.setData("");
        return result;
    }

    /**
     * 执行失败
     */
    public static <T> R<T> failed(RCode code) {
        R<T> result = new R<>();
        result.setCode(code.getCode());
        result.setMsg(code.getMsg());
        result.setData(null);
        return result;
    }

    /**
     * 执行失败
     *
     * @param code 响应码
     * @param msg  描述信息
     */
    public static R failed(int code, String msg) {
        R result = new R();
        result.setCode(code);
        result.setMsg(msg);
        result.setData("");
        return result;
    }

    /**
     * 执行失败
     *
     * @param msg 描述信息
     */
    public static <T> R<T> failed(String msg) {
        R<T> result = new R<>();
        result.setCode(-1);
        result.setMsg(msg);
        return result;
    }

    /**
     * 自定义响应
     *
     * @param code 响应码
     * @param msg  描述信息
     * @param data 返回结果集
     * @return
     */
    public static <T> R<T> json(Integer code, String msg, T data) {
        R<T> result = new R<T>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
